package com.kedou.sdk.ui.fragment;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Process;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;

import com.kedou.sdk.JuHaoWanSDK;
import com.kedou.sdk.core.HttpEngineApi;
import com.kedou.sdk.params.bean.ExitPopup;
import com.kedou.sdk.utils.WL;
import com.squareup.picasso.Picasso;

public class ExitAppDialogFragment extends BaseDialogFragment implements View.OnClickListener, Handler.Callback {

    private static final String TAG = ExitAppDialogFragment.class.getSimpleName();


    private Button mExitAppConfirmButton;
    private Button mExitAppCancelButton;
    private Handler mHandler;
    private ImageView mExitAppAdvertView;

    private int defaultImageId;
    private ExitPopup mExitPopup;

    public static void show(FragmentManager fragmentManager) {
        new ExitAppDialogFragment().show(fragmentManager, TAG);
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mHandler = new Handler(Looper.getMainLooper(), this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("wan_dialog_exit_app"));
        mExitPopup = HttpEngineApi.getInstance().getEngineConduits().getPlatformConfigInfo().getGame_exit();
        WL.d(TAG, "onCreateView ==========>");
        defaultImageId = getDrawableIdentifier("wan_dialog_advertisement_image");

        mExitAppConfirmButton = contentView.findViewById(getViewIdentifier("dialog_exitapp_confim_btn"));
        mExitAppCancelButton = contentView.findViewById(getViewIdentifier("dialog_exitapp_cancel_btn"));
        mExitAppAdvertView = contentView.findViewById(getViewIdentifier("dialog_exitapp_content_img"));
        setAdvertImageUrl(mExitPopup==null?"":mExitPopup.getImageUrl());
        mExitAppConfirmButton.setOnClickListener(this);
        mExitAppCancelButton.setOnClickListener(this);
        mExitAppAdvertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String url = mExitPopup != null ? mExitPopup.getJumpUrl() : "";
                    WL.d(TAG, "firsturl: " + url);
                    url = TextUtils.isEmpty(url) ? "https://www.baidu.com" : url;
                    WL.d(TAG, "secondurl: " + url);
                    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                } catch (ActivityNotFoundException exception) {
                    WL.d(TAG, "ActivityNotFoundException: " + exception.getLocalizedMessage());
                }
            }
        });



        return contentView;
    }

    private void setAdvertImageUrl(String imageUrl) {
        if (TextUtils.isEmpty(imageUrl)) {
            this.mExitAppAdvertView.setVisibility(View.GONE);
            this.mExitAppAdvertView.setImageResource(defaultImageId);
        } else {
            Picasso.with(getContext()).load(imageUrl).into(mExitAppAdvertView);
        }
    }

    @Override
    public void onClick(View view) {
        if (mExitAppConfirmButton == view) {
            mHandler.removeMessages(0);
            mHandler.sendEmptyMessageDelayed(0, 150);
        }
        dismiss();
    }

    @Override
    public boolean handleMessage(@NonNull Message msg) {
        switch (msg.what) {
            case 0:
                JuHaoWanSDK.getInstance().logout();
                JuHaoWanSDK.getInstance().destroy();
                if (getActivity() != null) {
                    getActivity().finish();
                }
                mHandler.removeMessages(1);
                mHandler.sendEmptyMessageDelayed(1, 100);
                break;

            default:
                Process.killProcess(Process.myPid());
                break;
        }
        return true;
    }

}
